<?php 

require_once '../functions.php';

xiu_get_current_user();


function add(){
//校验
if (empty($_POST['title']) 
  || empty($_POST['content'])
  || empty($_POST['slug'])
  || empty($_POST['created'])
  || empty($_POST['status'])
  || empty($_POST['category'])
) {
  $GLOBALS['message'] = '缺少必要参数';
  return false;
}
//============================
//文件校验
if(empty($_FILES['feature']) && $_FILES['feature']['error'] !== 0 ) {
  $GLOBALS['message'] = '请上传文件';
  return false;
}
//文件接收
$fetures = $_FILES['feature'];
$ect = pathinfo($fetures['name'],PATHINFO_EXTENSION);
$target = '../static/uploads/'. uniqid() . '.' . $ect;
if (!move_uploaded_file($fetures['tmp_name'] , $target)) {
  $GLOBALS['message'] = '保存文件失败';
  return false;
}
//文件保存
$file = substr($target, 2);
//接收
$slug  = $_POST['slug'];
$title  = $_POST['title'];
$feture = isset($file) ? $file : '';
$created  = $_POST['created'];
$content  = $_POST['content'];
$status  = $_POST['status'];
$user_id = $current_user['id'];
$category_id  = $_POST['category'];

//保存
$rows = sprintf(
      "insert into posts values (null, '%s', '%s', '%s', '%s', '%s', 0, 0, '%s', %d, %d)",
      $slug,
      $title,
      $feture,
      $created,
      $content,
      $status,
      $user_id,
      $category_id
    );
if (xiu_execute($rows) !== 1) {
  $GLOBALS['message'] = '保存失败';
  return;
}

//跳转
header('Location:posts.php');




}



if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  add();
}

//查询数据
$categories = xiu_fetch_all('select * from categories;');

 ?>




<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="utf-8">
  <title>Add new post &laquo; Admin</title>
  <link rel="stylesheet" href="/static/assets/vendors/bootstrap/css/bootstrap.css">
  <link rel="stylesheet" href="/static/assets/vendors/font-awesome/css/font-awesome.css">
  <link rel="stylesheet" href="/static/assets/vendors/nprogress/nprogress.css">
  <link rel="stylesheet" href="/static/assets/css/admin.css">
  <script src="/static/assets/vendors/nprogress/nprogress.js"></script>
</head>
<body>
  <script>NProgress.start()</script>

  <div class="main">
    <?php include 'inc/navbar.php' ?>
    <div class="container-fluid">
      <div class="page-title">
        <h1>写文章</h1>
      </div>
      <!-- 有错误信息时展示 -->
      <?php if (isset($message)): ?>
        <div class="alert alert-danger">
        <strong>错误！</strong><?php echo $message ?>
      </div>
      <?php endif ?>
      <form class="row" action="<?php echo $_SERVER['PHP_SELF'] ?>" method = 'post' enctype = 'multipart/form-data'>
        <div class="col-md-9">
          <div class="form-group">
            <label for="title">标题</label>
            <input id="title" class="form-control input-lg" name="title" type="text" placeholder="文章标题" value="<?php echo isset($_POST['title'])? $_POST['title']:'' ?>">
          </div>
          <div class="form-group">
            <label for="content">标题</label>
            <textarea id="content" class="form-control input-lg" name="content" cols="30" rows="10" placeholder="内容" ><?php echo isset($_POST['content']) ? $_POST['content'] : ''; ?></textarea>
          </div>
        </div>
        <div class="col-md-3">
          <div class="form-group">
            <label for="slug">别名</label>
            <input id="slug" class="form-control" name="slug" type="text" placeholder="slug" value="<?php echo isset($_POST['slug'])? $_POST['slug']:'' ?>">
            <p class="help-block">https://zce.me/post/<strong>slug</strong></p>
          </div>
          <div class="form-group">
            <label for="feature">特色图像</label>
            <!-- show when image chose -->
            <img class="help-block thumbnail" style="display: none">
            <input id="feature" class="form-control" name="feature" type="file" accept="image/*">
          </div>
          <div class="form-group">
            <label for="category">所属分类</label>
            <select id="category" class="form-control" name="category">
              <?php foreach ($categories as $item): ?>
                <option value="<?php echo $item['id']; ?>"><?php echo $item['name']; ?></option>
              <?php endforeach ?>
            </select>
          </div>
          <div class="form-group">
            <label for="created">发布时间</label>
            <input id="created" class="form-control" name="created" type="datetime-local" value="<?php echo isset($_POST['created'])? $_POST['created']:'' ?>">
          </div>
          <div class="form-group">
            <label for="status">状态</label>
            <select id="status" class="form-control" name="status">
              <option value="drafted">草稿</option>
              <option value="published">已发布</option>
              <option value="trashed">回收站</option>
            </select>
          </div>
          <div class="form-group">
            <button class="btn btn-primary" type="submit">保存</button>
          </div>
        </div>
      </form>
    </div>
  </div>
 <?php $current_page = 'post-add'; ?>
  <?php include 'inc/sidebar.php'; ?>

  <script src="/static/assets/vendors/jquery/jquery.js"></script>
  <script src="/static/assets/vendors/bootstrap/js/bootstrap.js"></script>
  <script>NProgress.done()</script>
</body>
</html>
